package array;

/**
 * @description: 977.有序数组的平方
 * https://leetcode.cn/problems/squares-of-a-sorted-array/description/
 * @author: caoyihong
 * @date: 2025-10-09 11:19
 * @Version 1.0
 **/
public class SquaresArray {
    public static void main(String[] args) {
        String[] inputs = args[0].substring(1,args[0].length()-1).split(",");
        int[] nums = new int[inputs.length];
        for (int i = 0; i < inputs.length; i++) {
            nums[i] = Integer.parseInt(inputs[i]);
        }

        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i] * nums[i];
        }
        int[] results = new int[nums.length];
        int i = 0, j = nums.length-1;
        int cur = nums.length-1;
        while (i <= j) {
            if (nums[i] < nums[j]) {
                results[cur] = nums[j];
                j--;
            } else {
                results[cur] = nums[i];
                i++;
            }
            cur--;
        }
        // 输出数组
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int c = 0; c < results.length; c++) {
            sb.append(results[c]);
            if (c < results.length-1) {
                sb.append(",");
            }
        }
        sb.append("]");
        System.out.println(sb);
    }
}
